import java.util.*;
import java.io.*;

public class Jugar {
	public static void leerFichero(String nombreFichero, Vector<Item> v) {
		FileInputStream fichero;
		byte[] texto;
		String s;
		StringTokenizer st;
		String clave, def;
		try {
			fichero = new FileInputStream(nombreFichero);
			texto = new byte[fichero.available()];
			fichero.read(texto);
			fichero.close();
			s = new String(texto);
			st = new StringTokenizer(s, "\t\n");
			try {
				for (;;) {
					clave = st.nextToken();
					def = st.nextToken();
					v.add(new Sinonimo(clave, def));
				}
			} catch (Exception e) {
			}
		} catch (Exception e) {
			System.out.println(e);
		}
	}

	private static int insertaClaves(Vector<Item> v, TablaDispersion taboa) {
		int colisions = 0;
		for (int i = 0; i < v.size(); i++) {
			colisions += taboa.insertar(v.get(i));
		}
		return colisions;
	}

	private static void imprimeColisions(Vector<Item> v, TablaDispersion taboa) {
		//Tempo que tarda en buscar todolos items
		Crono c = new Crono();
		int i = 0;
		long t = 0;
		int colisions = insertaClaves(v, taboa);
		
		c.iniciar();
		for (i = 0; i < v.size(); i++)
			taboa.buscar(v.get(i).clave());
		t = c.tiempo();
		
		
		System.out.print(Format.format(taboa + " (lambda = " + (float) v.size() / taboa.dimension() + "): ", 60, true));
		System.out.print(Format.format(""+colisions, 10, false));
		System.out.println(Format.format(t, 17)+" ms");
	}

	public static void proba(Vector<Item> v) {
		TablaDispersion taboa = new Cuadratica(38197, new DispersionB());
		insertaClaves(v, taboa);

		String[] vocablos = { "a" };
		for (int i = 0; i < vocablos.length; i++) {
			Item item = taboa.buscar(vocablos[i]);
			if (item != null)
				System.out.println(item);
			else
				System.out.println("'" + vocablos[i] + "' non esta no diccionario");
		}
	}

	public static void main(String[] args) {
		if (args.length != 1) {
			System.out.println("Uso: java Jugar 'nombre del fichero'");
			System.exit(-1);
		}

		Vector<Item> v = new Vector<Item>();
		leerFichero(args[0], v);

	//	proba(v);

		System.out.print(Format.format("", 60, false));
		System.out.print(Format.format("colisions", 10, false));
		System.out.println(Format.format("tempo busqueda", 20, false));
		
		
		imprimeColisions(v, new Abierta(19069, new DispersionA()));
		imprimeColisions(v, new Abierta(19069, new DispersionB()));

		imprimeColisions(v, new Lineal(25463, new DispersionA()));
		imprimeColisions(v, new Lineal(25463, new DispersionB()));

		imprimeColisions(v, new Cuadratica(38197, new DispersionA()));
		imprimeColisions(v, new Cuadratica(38197, new DispersionB()));
	}
}
